package com.qclix.inv.textui;

import com.qclix.inv.Client;
import com.qclix.inv.InvoiceService;
import com.qclix.inv.InvoiceServiceException;

/**
 * Handler for new client addition
 */
public class NewClientHandler extends AbstractActionHandler {

    private InvoiceService invoiceService;

    public void setInvoiceService(InvoiceService invoiceService) {
        this.invoiceService = invoiceService;
    }

    public void handle() {
        setNextHandler(FlowHandler.HANDLER.topMenu);
        Client client = new Client();
        System.out.println("Fill the client data");
        System.out.print("\tName: ");
        client.setName(readValue());
        System.out.print("\tAddress - street: ");
        client.setStreet1(readValue());
        System.out.print("\tAddress - street (2nd): ");
        client.setStreet2(readValue());
        System.out.print("\tAddress - zip: ");
        client.setZip(readValue());
        System.out.print("\tAddress - city: ");
        client.setCity(readValue());
        System.out.print("\tAddress - Country: (Belgique) ");
        client.setCountry(readValue("Belgique"));
        System.out.print("\tVAT: ");
        client.setVat(readValue());
        try {
            invoiceService.addClient(client);
        } catch (InvoiceServiceException e) {
            System.out.println("Oops, an error occurred while adding the client");
            return;
        }
        System.out.println("XMLClient " + client.getName() + " added successfully");
    }
}
